package com.lanp.springredis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author jiangpeng
 * Created with IntelliJ IDEA.
 * Description:  redis实例配置
 * Date: 2022-02-26 11:34:19
 */
//@Configuration
//@EnableAutoConfiguration
public class RedisConfig {
    /**
     * 商品redis
     */
    public static final String PRODUCT_REDIS_TEMPLATE = "productRedisTemplate"
            /**
             * 类目redis
             */
            , CATEGORY_REDIS_TEMPLATE = "categoryRedisTemplate";

    @Value("${redis.database.product}")
    private int productDatabase;
    @Value("${redis.database.category}")
    private int categoryDatabase;
    @Value("${redis.host}")
    private String host;
    @Value("${redis.nodes}")
    private List<String> nodes;
    @Value("${redis.sentinel-list}")
    private List<String> sentinel_list;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.timeout}")
    private int timeout;
    @Value("${redis.pool.max-idle}")
    private int maxIdle;
    @Value("${redis.pool.max-active}")
    private int maxActive;
    @Value("${redis.pool.min-idle}")
    private int minIdle;
    @Value("${redis.pool.max-wait}")
    private long maxWait;

    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        jedisPoolConfig.setMaxTotal(maxActive);
        return jedisPoolConfig;
    }

    /**
     * 获取redis单机地址和数据库配置
     *
     * @param database
     * @return
     */
    public RedisStandaloneConfiguration getRedisStandaloneConfig(int database) {
        //redis单机配置
        RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
        redisConfiguration.setDatabase(database);
        redisConfiguration.setPort(port);
        redisConfiguration.setHostName(host);
        redisConfiguration.setPassword(password);
        return redisConfiguration;
    }

    /**
     * 获取redis集群地址和数据库配置
     *
     * @param database
     * @return
     */
    public RedisClusterConfiguration getRedisClusterConfig(int database) {
        //redis单机配置
        RedisClusterConfiguration redisConfig = new RedisClusterConfiguration(nodes);
        redisConfig.setPassword(password);
        return redisConfig;
    }

    /**
     * 获取redis集群地址和数据库配置
     *
     * @param database
     * @return
     */
    public RedisSentinelConfiguration getRedisSentinelConfig(int database) {
        //redis单机配置
        RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();
        redisConfig.setDatabase(database);
        redisConfig.setMaster("mymaster");
        Set<RedisNode> redisNodes = new HashSet<>();
        sentinel_list.forEach(ipport -> {
            String[] split = ipport.split(":");
            redisNodes.add(new RedisNode(split[0], Integer.parseInt(split[1])));
        });
        redisConfig.setSentinels(redisNodes);
        redisConfig.setPassword(password);
        return redisConfig;
    }

    /**
     * 获取jedis客户端配置
     *
     * @return
     */
    public JedisClientConfiguration getJedisClientConfig() {
        return JedisClientConfiguration.builder()
                .usePooling().poolConfig(getJedisPoolConfig()).and().readTimeout(Duration.ofMillis(timeout)).build();
    }

    /**
     * 获取本连接设置库的redisTemplate对象实例
     *
     * @param database
     * @return
     */
    public RedisTemplate getRedisTemplateBean(int database) {
        //redis单机配置
        RedisSentinelConfiguration redisConfiguration = this.getRedisSentinelConfig(database);
        //redis连接池设置
        JedisClientConfiguration jedisClientConfiguration = this.getJedisClientConfig();
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisConfiguration, jedisClientConfiguration);
        connectionFactory.afterPropertiesSet();//执行连接池连接
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(connectionFactory);
        return stringRedisTemplate;
    }

    @Bean(name = PRODUCT_REDIS_TEMPLATE)
    public RedisTemplate getProductRedisTemplate() {
        return getRedisTemplateBean(productDatabase);
    }

    @Bean(name = CATEGORY_REDIS_TEMPLATE)
    public RedisTemplate getCategoryRedisTemplate() {
        return getRedisTemplateBean(categoryDatabase);
    }

}
